<html>
<head>
  <title>SQL Interface</title>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  <link rel="stylesheet" type="text/css" href="mystyle.css"/>
  <script type="text/javascript" src="jquery.js"></script>
  <script type="text/javascript" src="menu.js"></script>
  <script type="text/javascript">
    $(document).ready(function() { buildMenus(); });
  </script>
  <script type="text/javascript">var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));</script>
  <script type="text/javascript">try {var pageTracker = _gat._getTracker("UA-7674244-2");pageTracker._trackPageview();} catch(err) {}</script>
  <style type="text/css">#error {color:red;}</style>
  <script type="text/javascript">
    //Press the Run button if the user pressed F9 (this is the same as the run button in TOAD and SQL Developer)
    function runSQL(event)
    {
      var unicode = event.keyCode? event.keyCode : event.charCode;
      if(unicode == 120)
      {
        document.getElementById("run").click();
      }
    }
  </script>
  <script type="text/javascript" src="querySQL.js"></script>


</head>
<body onload="document.getElementById('query').focus();">
  <div id="content">
    <form method="post" action="query.php">
      <h3>SQL Interface</h3>
      Run SQL statements against the same database used to create the contents of this website.
      All data is from January 29, 2010.  Use the pre-built queries below, or see <a href="schemaDescription.html">this page</a> if
      you want to write your own queries.
      
      <br>
      <br>
        <form name="sqlForm" action="">

          Enter your case-sensitive lender_id (use <a href="http://kiva.org/myLenderId">kiva.org/myLenderId</a> to find it): <input type="text" size=30 id="LenderID"/ value="<?php echo $_POST["LenderID"];?>">
          <br>

          <table>
            <tr><td>Lender</td><td>Loan</td><td>Other</td></tr>
            <tr>
              <td>
                <select name="lender" onchange="setQuery(this)">
                  <option>- Please select -</option>
                  <option>My Lender Data</option>
                  <option>Lenders In My City</option>
                  <option>My Lender Rank</option>
                  <option>Similar Lenders</option>
                </select>
              </td>
              <td>
                <select name="loan" onchange="setQuery(this)">
                  <option>- Please select -</option>
                  <option>My Loans</option>
                  <option>Loan History Chart</option>
                  <option>My Countries (map)</option>
                </select>
              </td>
              <td>
                <select name="other" onchange="setQuery(this)">
                  <option>- Please select -</option>
                  <option>Average Loan Size</option>
                  <option>Average Loan Term</option>
                  <option>Borrowers For A Specific Loan</option>
                  <option>Lenders With Most Loans</option>
                  <option>Loans Disbursed First Or Funded First</option>
                  <option>Loans Funded (Activity)</option>
                  <option>Loans Per Country</option>
                  <option>Top Lenders Per Country</option>
                  <option>Users Without A Loan</option>
                  <option>Amount Given and Received</option>
                </select>
              </td>
            </tr>
          </table>
        <!--</form>-->
      <b>Enter your SQL statement below (click Run or press F9 to execute the query):</b><br>
      <input type="hidden" name="pageNumber" value="1"/>
      <textarea id="query" name="query" rows="10" cols="120" onKeyUp="runSQL(event)" tabindex="0"><?php if(!is_null($_POST["query"])) echo stripslashes($_POST["query"]); else echo "select * from loan";?></textarea><br>
      <input type="submit" id="run" name="run" value="Run"/> <!--Note: add disabled="disabled" here to turn off button --!>
      <br>
      <br>
      <?php
        //If SQL was entered send it to the database
        if(!is_null($_POST["query"]))
        {
          $ch = curl_init();
          //Old IP before 16-Sep-2009
          //curl_setopt($ch, CURLOPT_URL, "http://76.182.81.167:8080/kiva/query");

          //new ip?
          //curl_setopt($ch, CURLOPT_URL, "http://75.178.191.191:8080/kiva/query");

          //New mediacom IP
          //curl_setopt($ch, CURLOPT_URL, "http://173.27.169.212:8080/kiva/query");
          //curl_setopt($ch, CURLOPT_URL, "http://173.27.171.218:8080/kiva/query");
          //curl_setopt($ch, CURLOPT_URL, "http://24.12.162.106:8080/kiva/query");

          curl_setopt($ch, CURLOPT_URL, "http://173.29.153.71:8080/kiva/query");

          curl_setopt($ch, CURLOPT_POST, true);
          
          //Default pageNumber is 1, reset to 1 if run button is pressed, add 1 for next, subtract 1 for previous
          $pageNumber = 1;
          if(!is_null($_POST["run"])) { $pageNumber = 1; }
          if(!is_null($_POST["next"])) { $pageNumber = $_POST["pageNumber"] + 1; }
          if(!is_null($_POST["previous"])) { $pageNumber = $_POST["pageNumber"] - 1; }

          curl_setopt($ch, CURLOPT_POSTFIELDS, "query=".rawurlencode(stripslashes($_POST["query"]))."&pageNumber=".$pageNumber);
          //curl_setopt($ch, CURLOPT_HEADER, 0); 
          curl_exec($ch); 
          curl_close($ch);
        }
      ?>
    </form>
  </div>
  <div id="menu"></div>
</body>
</html>